//
//  MCDiscountSettingViewController.swift
//  StoreManage
//
//  Created by MC on 2017/12/19.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCDiscountSettingViewController: MCViewController {

    var discount = 0
    var discountRate = 0
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func confirmButtonClicked() {
        
        let discount = Double(onlineView.textField.text!)!
        let discountRate = Double(offlineView.textField.text!)!
        
        guard (discount >= 0.0 && discount <= 90.0) && (discountRate >= 0.0 && discountRate <= 100.0)  else {
            MCHUD_text(message: "请填写正确的优惠比例", view: self.view, delay: 1.2)
            return
        }
        
        let para = [
            "storeId"  : MCUserinfo_getStoreId(),
            "discount" : onlineView.textField.text ?? "",
            "discountRate" : offlineView.textField.text ?? "10"
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_updateStoreDiscount, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in

            MCHUD_text(message: "设置成功", view: self.view, delay: 1.5)
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5, execute: {
                self.navigationController?.popViewController(animated: true)
            })
        }, dislike: { (response) in }) { (error) in }
        
    }
    
    // MARK: - Network
    func sendNetworking() {
        
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "优惠设置"
        self.view.backgroundColor = UIColor.white
        
        onlineView.textField.text = String(self.discount)
        offlineView.textField.text = String(self.discountRate)
    }
    
    func initUI() {
        view.addSubview(offlineView)
        offlineView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(view)
            make.height.equalTo(100)
        }
        
        view.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(1)
            make.top.equalTo(offlineView.snp.bottom)
        }
        
        view.addSubview(onlineView)
        onlineView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(view)
            make.top.equalTo(lineView.snp.bottom)
            make.height.equalTo(100)
        }
        
        
        view.addSubview(confirmButton)
        confirmButton.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(view)
            make.height.equalTo(44)
        }
    }
    
    // MARK: - Setter & Getter
    
    lazy var offlineView: MCDiscountSettingItemView = {
        let view = MCDiscountSettingItemView()
        view.titleLabel.text = "线下优惠比例设置"
        view.unitLabel.text = "%"
        view.promptButton.setTitle(" 0-100，填5=5%=95折", for: UIControlState.normal)
        return view
    }()

    
    lazy var onlineView: MCDiscountSettingItemView = {
        let view = MCDiscountSettingItemView()
        view.titleLabel.text = "线上优惠比例设置"
        view.unitLabel.text = "%"
        view.promptButton.setTitle(" 0-90,填90=9.0折，最低9.0折", for: UIControlState.normal)
        return view
    }()
    
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var confirmButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.backgroundColor = UIColor.MCMain
        button.setTitle("保存", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(confirmButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()

}


class MCDiscountSettingItemView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.white
        
        self.addSubview(titleLabel)
        titleLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(self).offset(20)
            make.height.equalTo(40)
            make.top.equalTo(10)
        }
        
        self.addSubview(textField)
        textField.snp.remakeConstraints { (make) ->Void in
            make.centerY.equalTo(titleLabel)
            make.left.equalTo(titleLabel.snp.right).offset(10)
            make.size.equalTo(CGSize.init(width: 100, height: 30))
        }
        
        self.addSubview(unitLabel)
        unitLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(textField.snp.right).offset(5)
            make.centerY.equalTo(titleLabel)
        }
        
        self.addSubview(promptButton)
        promptButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(self).offset(-10)
            make.height.equalTo(40)
            make.bottom.equalTo(-10)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.black
        return label
    }()
    
    lazy var unitLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.black
        return label
    }()
    
    lazy var textField: UITextField = {
        let textField = UITextField()
        textField.backgroundColor = UIColor.MCGray_light
        textField.font = MCFont(14)
        textField.textAlignment = NSTextAlignment.center
        textField.keyboardType = UIKeyboardType.numberPad
        return textField
    }()
    
    lazy var promptButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(13)
        button.setImage(#imageLiteral(resourceName: "DiscountPay_star"), for: UIControlState.normal)
        button.setTitleColor(UIColor.MCRed, for: UIControlState.normal)
        return button
    }()
}

